#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @project : API_Service
# @File :   setupMain.py
# @Date :   2019/7/16 16:43
# @Author : 李文良

import os
import shutil

import pytest
from bin.config.confManage import dir_manage
from bin.script.logs import LogConfig

project_path = os.path.split(os.path.realpath(__file__))[0]
if ':' in project_path:
    project_path = project_path.replace('\\', '/')
else:
    pass

if __name__ == '__main__':
    LogConfig(project_path)
    from bin.script.writeCase import write_case
    from bin.script.jsonAlter import JsonAlter

    write_case(project_path + dir_manage('${api_dir}$'))
    shutil.rmtree(f'{project_path}/content/report/xml')
    # --workers=2 --tests-per-worker=4（2个进程并行，且每个进程最多4个线程运行，即总共最多8个线程运行）
    # args = ['-s', '--workers=2', '--tests-per-worker=4', '-q', '--alluredir',
    #         project_path + dir_manage('${report_xml_dir}$')]

    args = ['-s', '-q', '--alluredir',
            project_path + dir_manage('${report_xml_dir}$')]
    pytest.main(args)
    cmd = 'allure generate %s -o %s -c' % (project_path + dir_manage('${report_xml_dir}$'),
                                           project_path + dir_manage('${report_html_dir}$'))
    os.system('pip freeze > %s/environment.properties' % (project_path + dir_manage('${report_xml_dir}$')))

    os.system(cmd)
    shutil.copyfile(f'{project_path}/readme/categories.json', f'{project_path}/content/report/xml/categories.json')
    shutil.copyfile(f'{project_path}/readme/index.html', f'{project_path}/content/report/html/index.html')
    # 调用两个函数，更新内容
    w = JsonAlter()
    the_revised_dict = w.get_json_data("知蓝平台接口测试报告")
    w.write_json_data(the_revised_dict)
